<!--
 * 模板
 * @author luguoxiang
 * @date 2025-03-03
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <edit-container ref="containerRef" v-bind="containerConfig">
    <lg-form ref="formRef" v-loading="loading" :schema="schema" v-bind="formConfig">
      <template #type="{ data }">
        <t-select v-model="data.type" :options="typeList"/>
      </template>
      <template #content="{ data }">
        <template v-if="data.type === 0">
          <lg-upload v-model="data.content" value-type="url"/>
        </template>
        <template v-else-if="data.type === 2">
          <lg-wangeditor v-model="data.content"/>
        </template>
        <template v-else>
          <t-textarea v-model="data.content" />
        </template>
      </template>
    </lg-form>
  </edit-container>
</template>

<script setup lang="ts">
import {computed, ref, watch} from 'vue';
import { EditContainer, useEditContainer } from '@/components';
import {LgForm, useDictList} from '@/soar';
import api, { subject } from './api';
import schema from './schema';
import LgUpload from "@/soar/components/LgUpload/index.vue";
import LgWangeditor from "@/soar/components/LgWangeditor/index.vue";
import groupApi from './group/api';
import {date} from "@/utils/format";

const emits = defineEmits(['ok']);

const containerRef = ref();
const formRef = ref();
const { containerConfig, formConfig, loading, open, data } = useEditContainer<any>({
  header: subject,
  containerRef,
  formRef,
  getData: (params: any) => api.detail(params.id),
  onOk: async ({ values, data, params, isUpdate, changed }) => {
    if (changed) {
      values.id = data.id;
      await api.saveOrUpdate(values, isUpdate);
      emits('ok', values);
    }
  },
  containerProps: {
    width: '1200px',
  },
});

const { TemplateTypeEnum } = useDictList('TemplateTypeEnum')
const groupType = ref([])
watch(
  () => data.value.groupId,
  (v) => {
    if (v) {
      groupApi.detail(v).then((res: any) => {
        groupType.value = res.type;
        data.value.type = null;
      })
    } else {
      groupType.value = [];
    }
  },
  { immediate: true }
)

const typeList = computed(() => {
  if (groupType.value?.length) {
    return TemplateTypeEnum.value.filter((x: any) => groupType.value.includes(x.value))
  }
  return TemplateTypeEnum.value
})

defineExpose({ open });
</script>
